<?php


class reportClassAction extends Action
{
    /**
     * 按项目负责人统计
     * @url:/index.php?a=totalProjecter&m=report|main&d=report
     * @Auther: lv
     * @Email: 578530370@qq.com
     * @Date: 2020/3/23 11:33
     */
	public function totalProjecterAction()
    {
        $sdate = $this->request('sdate');
        $edate = $this->request('edate');
        $type = $this->request('type');
        $where = "1=1";
        if ($sdate) {
            $where .= " and a.addtime >= '".$sdate. " 00:00:00'";
        }
        if ($edate) {
            $where .= " and a.addtime <='". $edate. " 23:59:59'";
        }
        if ($type) {
            $where .= " and a.type=".$type;
        }
        if(empty($sdate) && empty($edate)) $where .= " and a.addtime >= '".date('Y'). "-01-01 00:00:00'";
        $where .= "  and c.status IN (1, 0) and c.modeid = 94";
        $sql = 'select count(a.projectadminer) as value,sum(a.budget) as budget,b.name,a.projectadminer as puid from [Q]zbcg_new_info as a inner join [Q]admin as b on a.projectadminer = b.id inner join [Q]flow_bill as c on a.mid = c.mid  where '. $where .' group by a.projectadminer';
        $lists = $this->db->getall($sql);
        return json_encode($lists, true);
    }

    /**
     * 按分类统计
     * @url:/index.php?a=totalType&m=report|main&d=report
     * @Auther: lv
     * @Email: 578530370@qq.com
     * @Date: 2020/3/23 11:33
     */
    public function totalTypeAction()
    {
        $sdate = $this->request('sdate');
        $edate = $this->request('edate');
        $where = "1=1";
        if ($sdate) {
            $where .= " and a.addtime >= '".$sdate. " 00:00:00'";
        }
        if ($edate) {
            $where .= " and a.addtime <='". $edate. " 23:59:59'";
        }
        if(empty($sdate) && empty($edate)) $where .= " and a.addtime >= '".date('Y'). "-01-01 00:00:00'";
        $where .= "  and c.status IN (1, 0) and c.modeid = 94";
        $sql = 'select count(a.type) as value,a.type as name,sum(a.budget) as budget from [Q]zbcg_new_info as a inner join [Q]flow_bill as c on a.mid = c.mid  where ' . $where . ' group by type';
        $lists = $this->db->getall($sql);
        $type = [];
//        $data = [];
        foreach ($lists as $key => $value) {
//            array_push($data, $value['num']);
            if(empty($value['name'])) {
                array_push($type, '其它');
            }else{
                array_push($type, $value['name']);
            }
        }
        $datas = array(
            'data' => $lists,
            'name' => $type
        );
        return json_encode($datas, true);
    }

    /**
     * 按代理公司统计
     * @url:/index.php?a=totalAgency&m=report|main&d=report
     * @Auther: lv
     * @Email: 578530370@qq.com
     * @Date: 2020/3/30 16:52
     */
    public function totalAgencyAction()
    {
        $sdate = $this->request('sdate');
        $edate = $this->request('edate');
        $type = $this->request('type');
        $where = "1=1";
        $where .= " and a.proxygov != ''";
        if ($sdate) {
            $where .= " and a.addtime >= '".$sdate. " 00:00:00'";
        }
        if ($edate) {
            $where .= " and a.addtime <='". $edate. " 23:59:59'";
        }
        if ($type) {
            $where .= " and a.type=".$type;
        }
        if(empty($sdate) && empty($edate)) $where .= " and a.addtime >= '".date('Y'). "-01-01 00:00:00'";
        $where .= "  and c.status IN (1, 0) and c.modeid = 94";
        $sql = 'select count(proxygov) as value,a.proxygov as name,sum(a.budget) as budget from [Q]zbcg_new_info as a inner join [Q]flow_bill as c on a.mid = c.mid where '. $where .' group by proxygov';
        $lists = $this->db->getall($sql);
        return json_encode($lists, true);
    }

    /**
     * 采购业务统计
     * @url:/index.php?a=totalCompany&m=report|main&d=report
     * @Auther: lv
     * @Email: 578530370@qq.com
     * @Date: 2020/3/31 12:26
     */
    public function totalCompanyAction()
    {
        $sdate = $this->request('sdate');
        $edate = $this->request('edate');
        $type = $this->request('type');
        $where = "1=1";
        if ($sdate) {
            $where .= " and a.addtime >= '".$sdate. " 00:00:00'";
        }
        if ($edate) {
            $where .= " and a.addtime <='". $edate. " 23:59:59'";
        }
        if ($type) {
            $where .= " and a.type=".$type;
        }
        if(empty($sdate) && empty($edate)) $where .= " and a.addtime >= '".date('Y'). "-01-01 00:00:00'";
        $where .= "  and c.status IN (1, 0) and c.modeid = 94";
        $sql = 'select count(a.`id`) as `value`,b.`name`,a.`comid`,sum(a.budget) as budget from [Q]zbcg_new_info as a inner join [Q]company as b on a.comid = b.id inner join [Q]flow_bill as c on c.mid = a.mid where ' . $where . ' group by a.`comid`';
        $lists = $this->db->getall($sql);
        $name = [];
        foreach ($lists as $key => $value) {
            if(empty($value['name'])) {
                array_push($name, '其它');
            }else{
                array_push($name, $value['name']);
            }
        }
        $datas = array(
            'data' => $lists,
            'name' => $name,
        );
        return json_encode($datas, true);
    }
}